﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
using CheckClicker;
using System.Data.OleDb;

namespace CheckClicker
{
    public partial class frmtime : Form
    {
        CommunicationManager comm = new CommunicationManager();
        string transType = string.Empty;
        int minute = 0;
        int second = -1;
        int u = 1;


        #region "Biến cục bộ trong Form"

        private OleDbConnection objConnect;
        private DataTable objDataTable;
        private enum varMode : int { AddMode, EditMode, DeleteMode }
        #endregion

        #region "Tạo Kết nối và hủy kết nối"

        private void subCreateConnect()
        {
            String varChuoiConnect = "Provider = Microsoft.Jet.OLEDB.4.0 ;" +
                        "Data Source =" + Application.StartupPath + @"\Database.mdb;" +
                        "User ID = Admin";
            objConnect = new OleDbConnection(varChuoiConnect);
            objConnect.Open(); //Mở kết nối
        }

        private void subDestroyConnect()
        {
            objConnect.Close(); //Đóng kết nối
            objConnect.Dispose();//Giải phóng tài nguyên
            objConnect = null; //Hủy đối tượng
        }
        #endregion

        #region "Delete Table"
        private void DeleTable(string table)
        {
            //Tạo kết nối
            subCreateConnect();
            //Tạo command
            OleDbCommand objcommand = new OleDbCommand();
            objcommand.Connection = objConnect;
            objcommand.CommandType = CommandType.Text;
            objcommand.CommandText = "Delete From " + "" + table + "" ;
            objcommand.ExecuteNonQuery();
            //Hủy đối tượng
            objcommand.Dispose(); objcommand = null;
            subDestroyConnect();
        }
        #endregion

        #region "Write data to comport"
        private void Writedata(string msg)
        {
            subCreateConnect();
            //Truy vấn dữ liệu
            String varSelect = "Select * from Comport";//Lệnh SQL
            OleDbCommand objCommand = new OleDbCommand(varSelect, objConnect);
            //Tạo DataReader nhận dữ liệu trả về
            OleDbDataReader objReader = objCommand.ExecuteReader();
            objDataTable = new DataTable("abc");
            objDataTable.Load(objReader);
            DataRow jobRow = objDataTable.Rows[0];
            string name = jobRow["Name"].ToString();
            string parity = jobRow["Parity"].ToString();
            string stop = jobRow["Stop"].ToString();
            string data = jobRow["Data"].ToString();
            string baud = jobRow["Baud"].ToString();

            comm.OpenPort1(baud, data, stop, parity, name, msg);
        }
        #endregion

        public frmtime()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "0 : 0";
        }
       
        private void button1_Click_1(object sender, EventArgs e)
        {
            
            if (u == 1)
            {
               DeleTable("Chart");
               comm.Reset();
               DeleTable("Report");
               string start = "STAT123456789";
               Writedata(start);
               timer1.Enabled = true;
               button1.Text = "Stop";
               u = 0;
                
            }
            else
            {
                //Push Stop
                comm.Stop = "STOP";
                string stop = "STOP123456789";
                Writedata(stop);
                comm.Result();
                label2.Text =Convert.ToString(comm.sum1) + " People";
                timer1.Enabled = false;
                button1.Text = "Start";
                u = 1;
                second = -1;
                minute = 0;
                //timer1_Tick_1(null, null);
                //timer1.Enabled = false;


            }
            
        }
           

        private void timer1_Tick_1(object sender, EventArgs e)
        {
                //int minute = 0;
                //int second = -1;
                ++second;
                label1.Text = minute.ToString() + " : " + second.ToString();
                label2.Text = Convert.ToString(comm.sum1) + "People";
                
                if (second == 59)
                {
                    second = -1;
                    minute++;
                }
            }
        
        

        private void timer2_Tick(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            DeleTable("AddID");
            comm.closePort();
            this.Hide();
            frmMain frm = new frmMain();
            frm.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //frmDisplay frm = new frmDisplay();
            //frm.ShowDialog();
            comm.NoAnswer();
        }

       
    }
}



